<template>
  <a-modal
    v-model:open="openDeleteSpace"
    title="删除空间"
    ok-text="确定"
    cancel-text="取消"
    @ok="deleteSpace"
  >
    <p>你确定要删除该空间？</p>
  </a-modal>
</template>

<script setup lang="ts">

import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { deleteSpaceUsingPost } from '@/api/spaceController.ts'

const openDeleteSpace = ref(false)
let deleteId = ref<number>(0)
// 获取父组件方法
const emit = defineEmits(['refresh'])

// 打开对话框
const openDialog = (id: number) => {
  deleteId.value = id
  openDeleteSpace.value = true
}

// 删除空间
const deleteSpace = async () => {
  if (!deleteId.value) {
    return
  }
  const res = await deleteSpaceUsingPost({ id: deleteId.value })
  if (res.data.code === 0 && res.data.data) {
    message.success('删除成功')
    openDeleteSpace.value = false
    await emit('refresh')
  } else {
    message.error('删除失败，' + res.data.message)
    openDeleteSpace.value = false
  }
}

// 暴露方法
defineExpose({
  openDialog
});

</script>

<style scoped>

</style>
